home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 27.zip
/
BS1 part 27
/
HiSoftBasicD2.adf
/
AllocExample.bas
< prev
next >
Wrap
BASIC Source File
|
1989-05-17
|
1KB
|
34 lines
' example showing how to safely allocate and free memory from HiSoft BASIC
' for the more advanced programmer only
' SafeAlloc takes the same parameters as AllocMem in the exec.library
' but adds the memory to a special list. This means it will always
' deallocate the memory when the program finishes or stops with a
' run-time error. If there is not enough memory then a BASIC
' out-of-memory error will occur.
' SafeFree takes the result of a previous SafeAlloc call and returns
' the memory block to the system. It only has an address parameter.
' If you try to give back memory that was not allocated with SafeAlloc
' then a fatal error 'memory list corrupt' will occur.
' If you pass 0 to this routine, no action will be taken. This makes
' clean-up code a lot easier.
LIBRARY "hisoftbasic.library"
DECLARE FUNCTION SafeAlloc&(length&,type&) LIBRARY
DECLARE SUB SafeFree&(addr&) LIBRARY
' useful options for type&
CONST MEM_PUBLIC=1, MEM_CHIP=2, MEM_FAST=4
MEM_CLEAR=&h10000 'cannot be a CONST
' ultra-simple example
block&=SafeAlloc&(40,MEM_PUBLIC OR MEM_CHIP)
PRINT "Got some public chip memory at";block&
SafeFree block&
PRINT "Given back safely"